package cn.boge.org.controller;

import cn.boge.org.domain.Employee;
import cn.boge.org.domain.Shop;
import cn.boge.org.query.EmpQuery;
import cn.boge.org.query.ShopQuery;
import cn.boge.org.service.EmployeeService;
import cn.boge.org.service.ShopService;
import cn.boge.org.utils.AjaxResult;
import cn.boge.org.utils.QueryResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RequestMapping("/shop")
@RestController
@Api(tags = "商铺维护接口")
//@CrossOrigin
public class ShowController {

    @Autowired
    private ShopService shopService;



    @GetMapping("/{id}")
    public Shop findById(@PathVariable("id") Long id){
        return shopService.findById(id);
    }

    @GetMapping
    public List<Shop> findAll(){
        List<Shop> shops = shopService.findAll();
        //shops.forEach(System.out::println);
        return shops;

    }

    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id")Long id){
        try {
            shopService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    @PutMapping
    public AjaxResult addOrUpt(@RequestBody Shop shop){
        try {
            if(shop.getId()==null){
                shopService.add(shop);
            }else {
                shopService.upt(shop);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    @PostMapping//根据条件查询
    public QueryResult<Shop> findByQuery(@RequestBody ShopQuery Query){
        return shopService.findByQuery(Query);
    }

    @PatchMapping
    public AjaxResult delpatch(@RequestBody List<Long> ids){
        try {
            shopService.delpatch(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }
}
